package com.tjetc.adminmanage;

import java.util.List;
import java.util.Scanner;

import com.tjetc.domian.Admin;
import com.tjetc.proxy.ClientProxy;
import com.tjetc.service.AdminService;
import com.tjetc.vo.Result;

//管理员管理
public class AdminManageView {
    private static Scanner scanner = new Scanner(System.in);
   private AdminService adminService= ClientProxy.getProxyInstance(AdminService.class);
    public void start() {
        while (true) {
            System.out.println("管理员管理菜单");
            System.out.println("1.查询管理员信息");
            System.out.println("2.添加管理员");
            System.out.println("3.修改管理员信息");
            System.out.println("4.删除管理员");
            System.out.println("5.按用户名搜索管理员");
            System.out.println("0.返回上一级");
            int i = scanner.nextInt();
            switch (i) {
                case 1:
                    list();
                    break;
                case 2:
                    add();
                    break;
                case 3:
                    update();
                    break;
                case 4:
                    delete();
                    break;
                case 5:
                    searchByUsername();
                    break;
                case 0:
                    return;
                default:
                    System.out.println("输入有误，请重新输入");
            }
        }

    }

    private void delete() {
        System.out.println("请输入管理员编号:");
        int id = scanner.nextInt();
        Result result = adminService.delete(id);
        System.out.println(result.getMsg());
    }

    private  void list() {
        List<Admin> list = adminService.list();
        if (list == null || list.isEmpty()) {
            System.out.println("没有数据");
        }else {
            for (Admin admin : list) {
                System.out.println(admin);
            }
        }
    }
    private  void add() {
        System.out.println("请输入管理员名称");
        String username = scanner.next();
        System.out.println("请输入管理员密码");
        String password = scanner.next();
        Result result = adminService.add(new Admin(username, password));
        System.out.println(result.getMsg());
    }
    private  void update() {
        System.out.println("请输入要修改的id");
        int id = scanner.nextInt();
        System.out.println("请输入要修改的用户名");
        String name = scanner.next();
        System.out.println("请输入要修改的密码");
        String password = scanner.next();
        Result result = adminService.update(new Admin(id, name, password));
        System.out.println(result.getMsg());
    }

    private void searchByUsername() {
        System.out.println("请输入要搜索的管理员用户名（支持模糊搜索）：");
        String username = scanner.next();
        List<Admin> admins = adminService.findByUsernameLike(username);
        if (admins == null || admins.isEmpty()) {
            System.out.println("未找到匹配的管理员信息");
        } else {
            System.out.println("查询结果：");
            for (Admin admin : admins) {
                System.out.println(admin);
            }
        }
    }

}
